
#include <iostream>

#include "jell/netjell.hpp"

#include "jell/jell_address.hpp"

using namespace jell;
using namespace std;

int main(int argc, char* argv[])
{
    const char* pszAddress = "tcp://127.0.0.1:4344";
    JellAddress address( pszAddress );

    NetJell*    jell_p = NetJell::create();
    try {
        jell_p->listen( address );
    }
    catch(JellException& e) {
        cerr << e << endl;
    }

    const unsigned  BUF_SIZE = 65536;
    Buf*    buf_p = jell_p->allocator().allocate( BUF_SIZE );
    while( true ) {
        JellAddress peer_address;
        try {
            jell_p->recvfrom(peer_address, *buf_p);
            cout << " from: " << peer_address.uri()
                 << " data: " << buf_p->data()
                 << endl;
        }
        catch(JellException& e) {
            cerr << e << endl;
            break;
        }

        try {
            jell_p->sendto(peer_address, *buf_p);
        }
        catch(JellException& e) {
            cerr << e << endl;
            break;
        }
    }
    jell_p->allocator().deallocate( buf_p );

    try {
        jell_p->shutdown( address );
    }
    catch(JellException& e) {
        cerr << e << endl;
    }

    delete jell_p;

    return  0;
}
